package com.huan.sort;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class FlowBean implements WritableComparable<FlowBean> {

    private long upFlow;
    private long downFlow;
    private long sumFlow;

    public FlowBean() { }

    public FlowBean(long upFlow, long downFlow) {
        this.upFlow = upFlow;
        this.downFlow = downFlow;
        this.sumFlow = upFlow+downFlow;
    }

    public long getUpFlow() {
        return upFlow;
    }

    public void setUpFlow(long upFlow) {
        this.upFlow = upFlow;
    }

    public long getDownFlow() {
        return downFlow;
    }

    public void setDownFlow(long downFlow) {
        this.downFlow = downFlow;
    }

    public long getSumFlow() {
        return sumFlow;
    }

    public void setSumFlow(long sumFlow) {
        this.sumFlow = sumFlow;
    }

    @Override
    public String toString() {
        return upFlow +
                "\t" + downFlow +
                "\t" + sumFlow ;
    }

    //TODO 核心比较条件
    @Override
    public int compareTo(FlowBean bean) {
        //TODO 核心比较条件判断
        //TODO 正向排序法则
        int result ;  // 0 , 1 , -1  相等取0 正向取1 , 负向取-1
        if (sumFlow > bean.getSumFlow()){
            result = -1;
        }else if (sumFlow < bean.getSumFlow()){
            result = 1;
        }else {
            result = 0;
        }

        return result;
    }

    //TODO 序列化
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeLong( upFlow );
        out.writeLong( downFlow );
        out.writeLong( sumFlow );
    }

    //TODO 反序列化
    @Override
    public void readFields(DataInput in) throws IOException {
        upFlow = in.readLong();
        downFlow = in.readLong();
        sumFlow = in.readLong();
    }
}
